﻿Imports System.Data.SqlClient

Public Class CPhieuNhap
    Dim _iMaPhieuNhap As Integer
    Dim _iMaNhanVien As Integer
    Dim _strNgayNhap As String
    Shared da As New DataProvider
    Property MaPhieuNhap As Integer
        Get
            Return _iMaPhieuNhap
        End Get
        Set(ByVal value As Integer)
            _iMaPhieuNhap = value
        End Set
    End Property
    Property MaNhanVien As Integer
        Get
            Return _iMaNhanVien
        End Get
        Set(ByVal value As Integer)
            _iMaNhanVien = value
        End Set
    End Property
    Property NgayNhap As String
        Get
            Return _strNgayNhap
        End Get
        Set(ByVal value As String)
            _strNgayNhap = value
        End Set
    End Property
    Public Shared Function LoadDS() As DataTable
        Return da.ShowData("select * from PHIEUNHAPKHO")
    End Function
    Public Shared Sub Insert(ByVal pn As CPhieuNhap, ByVal mh As CMatHang)
        Try
            CMatHang.Insert(mh)
            da.OpenConnect()
            Dim sql = "INSERT INTO PHIEUNHAPKHO(NgayNhap,MaNhanVien) values('" + pn.NgayNhap + "','" + pn.MaNhanVien + "')"
            Dim cmd As New SqlCommand(sql, da.con)
            cmd.ExecuteNonQuery()
            Dim _strMaPhieuNhap As String = da.GetData("select (MAX)MaPhieuNhap from PHIEUNHAPKHO")
            da.OpenConnect()
            sql = "INSERT INTO CTPHIEUNHAPKHO VALUES(@MaPhieuNhap,@MaSP,@MaDonVi,@SoLuong,@DonGia)"
            cmd = New SqlCommand(sql, da.con)
            cmd.Parameters.AddWithValue("@MaPhieuNhap", _strMaPhieuNhap)
            cmd.Parameters.AddWithValue("@MaSP", mh.MaMH)
            cmd.Parameters.AddWithValue("@MaDonVi", mh.DonVi)
            cmd.Parameters.AddWithValue("@SoLuong", mh.SoLuong)
            cmd.Parameters.AddWithValue("@DonGia", mh.Gia)
            cmd.ExecuteNonQuery()
            MsgBox("Thêm thành công", MsgBoxStyle.Information, "Thông báo")

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Thông báo")
        Finally
            da.CloseConnect()
        End Try

    End Sub
    Public Shared Sub Delete(ByVal MaPhieuNhap As String)
        Try

            da.OpenConnect()
            Dim sql = "DELETE FROM CTPHIEUNHAPKHO WHERE MaPhieuNhap = '" + MaPhieuNhap + "'"
            Dim cmd As New SqlCommand(sql, da.con)
            cmd.ExecuteNonQuery()
            sql = "DELETE FROM PHIEUNHAPKHO WHERE MaPhieuNhap = '" + MaPhieuNhap + "'"
            cmd = New SqlCommand(sql, da.con)
            cmd.ExecuteNonQuery()
            MsgBox("Xóa thành công", MsgBoxStyle.Information, "Thông báo")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Thông báo")
        Finally
            da.CloseConnect()
        End Try

    End Sub
    Public Shared Sub Update(ByVal pn As CPhieuNhap, ByVal mh As CMatHang)
        Try
            da.OpenConnect()
            Dim sql = "UPDATE PHIEUNHAPKHO " + _
                      "SET MaNhanVien = " + pn.MaNhanVien + " and NgayNhap = '" + pn.NgayNhap + "'" + _
                      "WHERE MaPhieuNhap = " + pn.MaPhieuNhap + ""
            Dim cmd As New SqlCommand(sql, da.con)
            cmd.ExecuteNonQuery()
            sql = "UPDATE CTPHIEUNHAPKHO " + _
                  "SET MaSanPham = '" + mh.MaMH + "' and MaDonVi = '" + mh.DonVi + "' and SoLuong = '" + mh.SoLuong + "' and DonGia = '" + mh.Gia + "'" + _
                  "WHERE MaPhieuNhap = '" + pn.MaPhieuNhap + "'"
            cmd = New SqlCommand(sql, da.con)
            cmd.ExecuteNonQuery()
            MsgBox("Cập nhật thành công", MsgBoxStyle.Information, "Thông báo")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Thông báo")
        Finally
            da.CloseConnect()
        End Try
    End Sub
End Class
